<template>
  <div>
    <hr />

    <el-select
      v-model="subjectsId"
      placeholder="请选择"
      style="margin-top: 20px; margin-bottom: 5px"
    >
      <el-option
        v-for="item in subjects"
        :key="item._id"
        :label="item.name"
        :value="item._id"
      ></el-option>
    </el-select>
    <el-table
      :data="classes"
      border
      style="width: 100%; margin-top: 20px; margin-bottom: 20px"
    >
      <el-table-column prop="_id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="班级名称"> </el-table-column>
      <el-table-column prop="subjectsId.name" label="所属专业">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageData.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions } = createNamespacedHelpers("classesModule");
const { mapState:subjectsState, mapActions:subjectsActions } = createNamespacedHelpers("subjectsModule");
export default {
  data() {
    return {
      // tableData: [],
      // subjects: [],
      form: {
        name: "",
        
      },
       subjectsId: "",
      pageData: {
        currentPage: 1,
        pageSize: 5,
      },
      // total: 0,
      searchData: {
        type: "",
      },
    };
  },
  created() {
    //获取所以专业数据
    this.getSubjectsAsync();
    this.getClasses();
    // this.getSubjects();
    
  },
  //侦听页码变化
  watch: {
    subjectsId(){
//根据专业获取班级
this.getClasses();
    },
    pageData: {
      handler() {
        this.getClasses();
      },
      deep: true,
    },
  },
  computed: {
    ...mapState(["classes", "total"]),
    ...subjectsState(["subjects"])
  },
  methods: {
    ...mapActions(["getClassesAsync","getClassesBySidAsync"]),
   ...subjectsActions(["getSubjectsAsync"]),
    //获取全部班级数据并渲染
    async getClasses() {
      if(this.subjectsId){
//根据专业获取班级
this.getClassesBySidAsync({subjectsId:this.subjectsId,...this.pageData});
      }else{
 this.getClassesAsync(this.pageData);
      }
     
      // const res = await this.$api.classes.get(this.pageData);
      // if (res.code) {
      //   console.log(res.data.rows);
      //   this.tableData = res.data.rows;
      //   this.total = res.data.total;
      // }
    },
    // 删除一个班级数据
    async handleDelete(row) {
      console.log(row);
      const res = await this.$api.classes.delete({ _id: row._id });
      console.log(res);
      if (res.code) {
        this.$message({
          message: "删除班级成功",
          type: "success",
        });
        this.getClasses();
      }
    },
    //获取全部专业
    // async getSubjects() {
    //   const res = await this.$api.subjects.get();
    //   console.log(res);

    //   if (res.code) {
    //     this.subjects = res.data.rows;
    //   }
    // },
    //点击跳转到修改页面
    handleEdit(row) {
      this.$router.push(`/home/classUpdate/${row._id}`);
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageData.pageSize = val;
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageData.currentPage = val;
    },
  },
};
</script>

<style></style>
